package com.qire.common.constant;

import com.qire.common.basic.DateUtil;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.model.enumeration.RewardType;
import com.qire.common.model.enumeration.SpecialEventType;
import com.qire.common.utils.CacheStorageUtil;

import java.util.Date;

/**
 * 应用启动时记录相关信息，需要在启动时完成的相关工作
 */
public final class StartupConfigInfo {

    public final static StartupConfigInfo INFO = new StartupConfigInfo();

    private PromotionalEvent promotionalEvent;
    private UserWelfareTask userLoginWelfareTask;
    private UserWelfareTask userBindWelfareTask;

    private StartupConfigInfo() {}

    /**
     * 加载促销活动信息（启动时促销活动信息），用于进入首页时展示弹窗活动
     */
    public void loadPromotionalEventInfo(boolean isEnable, SpecialEventType type, String imgUrl, int imgWidth, int imgHeight,
                                         String eventUrl, String eventTitle, int comicId, String startTime, String endTime) {
        this.promotionalEvent = new PromotionalEvent(isEnable, type, imgUrl, imgWidth, imgHeight, eventUrl, eventTitle, comicId, startTime, endTime);
    }

    /**
     * 加载用户福利人物信息（用户登录及绑定奖励等），用于首页底部任务奖励浮层提示，以及相关任外务触发
     */
    public void loadUserWelfareTaskInfo(RewardType loginRewardType, int loginRewardValue, RewardType bindRewardType, int bindRewardValue) {
        userLoginWelfareTask = new UserWelfareTask(loginRewardType, loginRewardValue);
        userBindWelfareTask = new UserWelfareTask(bindRewardType, bindRewardValue);
    }

    /**
     * 是否允许展示启动时弹窗活动: <br />
     *  1. 存在活动信息，<br />
     *  2. 活动处于启动状态 <br />
     *  3. 还没有展示过活动 <br />
     *  满足以上三点即为允许 <br />
     * @return 允许 true 否则 false
     */
    public boolean allowShowPromotionalEvent() {
        if(ObjectUtil.isNull(promotionalEvent)) {
            return false;
        }
        if(!promotionalEvent.isEnable) {
            return false;
        }
        return promotionalEvent.notShown() && promotionalEvent.isValidTimePeriod();
    }

    public PromotionalEvent promotionalEvent() {
        return promotionalEvent;
    }

    public UserWelfareTask userLoginWelfareTask() {
        return userLoginWelfareTask;
    }

    // todo: 目前已经取消了绑定奖励相任务奖励，2.4.5版本之后如果多个版本未追加回可移除相关业务逻辑
    @Deprecated
    public UserWelfareTask userBindWelfareTask() {
        return userBindWelfareTask;
    }


    /** 促销活动 */
    public class PromotionalEvent {
        public final boolean isEnable;
        public final SpecialEventType type;
        public final String  imgUrl;
        public final int     imgWidth;
        public final int     imgHeight;
        public final String  eventUrl;
        public final String  eventTitle;
        public final int     comicId;
        public final String  startTime;
        public final String  endTime;

        public PromotionalEvent(boolean isEnable, SpecialEventType type, String imgUrl, int imgWidth, int imgHeight,
                                String eventUrl, String eventTitle, int comicId, String startTime, String endTime) {
            this.isEnable   = isEnable;
            this.type       = type;
            this.imgUrl     = imgUrl;
            this.imgWidth   = imgWidth;
            this.imgHeight  = imgHeight;

            this.eventUrl   = eventUrl;
            this.eventTitle = eventTitle;
            this.comicId    = comicId;
            this.startTime  = startTime;
            this.endTime    = endTime;
        }

        public boolean isValidTimePeriod() {
            long dateStartTime  = DateUtil.yMdHms.parse(startTime).getTime();
            long dateEndTime    = DateUtil.yMdHms.parse(endTime).getTime();
            long nowTime        = System.currentTimeMillis();
            return nowTime < dateEndTime && nowTime > dateStartTime;
        }

        public boolean notShown() {
            switch (type) {
                case WebLinkPage :
                    return CacheStorageUtil.saveIfNotExist(CacheStorageKey.STARTUP_PROMOTIONAL_EVENT_IMAGE, imgUrl);
                case ComicReadPage :
                    return CacheStorageUtil.saveIfNotExist(CacheStorageKey.STARTUP_PROMOTIONAL_EVENT_COMIC, comicId);
                default : return true;
            }
        }
    }

    /** 用户福利任务 */
    public class UserWelfareTask {
        public final RewardType rewardType;
        public final int rewardValue;

        public UserWelfareTask(RewardType rewardType, int rewardValue) {
            this.rewardType     = rewardType;
            this.rewardValue    = rewardValue;
        }
    }
}
